unit CreateNewEvent;

interface

uses
  Color, Database, VarSelect, Sounds, Functions, VarGrabber, Groups,
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, DB, DBTables, StdCtrls, Buttons,
  ExtCtrls, Mask, Menus, ComCtrls, Grids, DBCtrls;

type
  TFormNewEvent = class(TForm)
    Panel1: TPanel;
    Status: TLabel;
    Bevel1: TBevel;
    Bevel6: TBevel;
    Label5: TLabel;
    Label9: TLabel;
    EventText: TLabel;
    Label12: TLabel;
    EditEvent: TEdit;
    BtnSetSound: TBitBtn;
    EditSound: TEdit;
    BtnPlaySound: TBitBtn;
    BtnEventText: TBitBtn;
    CheckCase: TCheckBox;
    EditVariables: TEdit;
    BtnVariables: TBitBtn;
    BtnClearBeep: TBitBtn;
    CheckTerminal: TCheckBox;
    CheckOriginal: TCheckBox;
    CheckDisabled: TCheckBox;
    CheckFiltered: TCheckBox;
    CheckRetain: TCheckBox;
    BtnHelp: TBitBtn;
    BtnCancel: TBitBtn;
    BtnSave: TBitBtn;
    BtnNew: TBitBtn;
    Label2: TLabel;
    Bevel2: TBevel;
    Bevel3: TBevel;
    Label1: TLabel;
    BtnGroups: TSpeedButton;
    MainMenu1: TMainMenu;
    Main1: TMenuItem;
    MenuHelp: TMenuItem;
    EditGroup: TDBComboBox;
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure CheckOriginalClick(Sender: TObject);
    procedure BtnHelpClick(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtnClearBeepClick(Sender: TObject);
    procedure BtnPlaySoundClick(Sender: TObject);
    procedure BtnSetSoundClick(Sender: TObject);
    procedure BtnEventTextClick(Sender: TObject);
    procedure BtnVariablesClick(Sender: TObject);
    procedure BtnNewClick(Sender: TObject);
    procedure BtnGroupsClick(Sender: TObject);
    procedure EditEventKeyPress(Sender: TObject; var Key: Char);
    procedure EditEventChange(Sender: TObject);
  private
    { Private declarations }
    procedure PopulateGroups;
  public
    { Public declarations }
  end;

var
  FormNewEvent: TFormNewEvent;

implementation

uses Main, DBClient;

{$R *.DFM}

procedure TFormNewEvent.FormCreate(Sender: TObject);
begin
  PopulateGroups;
end;

procedure TFormNewEvent.BtnGroupsClick(Sender: TObject);
var form: TFormGroups;
begin
  form:=TFormGroups.create(self);
  form.frommain:=false;
  form.showmodal;
  form.free;
  PopulateGroups;
end;

procedure TFormNewEvent.PopulateGroups;
var table: TClientDataset;
begin
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableGroups, false);
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      EditGroup.items.add(fieldbyname('FGroup').asstring);
      next;
    end;
  end;
  table.free;
end;

procedure TFormNewEvent.BtnHelpClick(Sender: TObject);
begin
  Datawell.help('new_event');
end;

procedure TFormNewEvent.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormNewEvent.CheckOriginalClick(Sender: TObject);
begin
  if CheckOriginal.checked then begin
    EventText.font.color:=clblack;
    EventText.color:=clbtnface;
  end
  else begin
    EventText.font.color:=claqua;
    EventText.color:=clteal;
  end;
end;

procedure TFormNewEvent.BtnCancelClick(Sender: TObject);
begin
  close;
end;

procedure TFormNewEvent.BtnClearBeepClick(Sender: TObject);
begin
  EditSound.text:='';
  EditSound.hint:='';
end;

procedure TFormNewEvent.BtnPlaySoundClick(Sender: TObject);
begin
  FormMain.PlaySound(EditSound.hint,1,0,0,false);
end;

procedure TFormNewEvent.BtnSetSoundClick(Sender: TObject);
var
  fullfile, work, sound: string;
  i: integer;
  form: TFormSounds;
begin
  form:=TFormSounds.create(self);
  form.showmodal;
  fullfile:=form.sound;
  form.free;
  if fullfile = '' then exit;
  work:=fullfile;
  delete(work,length(work)-3,4);
  i:=length(work);
  sound:='';
  while work[i] <> '\' do begin
    sound:=work[i]+sound;
    dec(i);
  end;
  EditSound.text:=sound;
  EditSound.hint:=fullfile;
  BtnEventText.setfocus;
end;

procedure TFormNewEvent.BtnEventTextClick(Sender: TObject);
var form: TFormColor;
begin
  form:=TFormColor.create(self);
  form.ColorGrid.foregroundindex:=Datawell.ColorFigure(EventText.font.color);
  form.ColorGrid.backgroundindex:=Datawell.ColorFigure(EventText.color);
  form.showmodal;
  if (form.findex >= 0) and (form.bindex >= 0) then begin
    if (form.findex = 0) and (form.bindex = 0) then begin
      EventText.font.color:=clblack;
      EventText.color:=clbtnface;
      CheckOriginal.checked:=true;
    end
    else begin
      CheckOriginal.checked:=false;
      EventText.font.color:=Datawell.FigureColor(form.findex);
      EventText.color:=Datawell.FigureColor(form.bindex);
    end;
  end;
  form.free;
  if BtnSave.visible then BtnSave.setfocus else BtnNew.Setfocus;
end;

procedure TFormNewEvent.BtnVariablesClick(Sender: TObject);
var
  form: TFormVarSelect;
  vars: TStringList;
  i, j: integer;
begin
  if pos('*',EditEvent.text) < 1 then begin
    messagedlg('There are no *''s in the event text to use variables with',
      mtinformation,[mbok],0);
    EditEvent.selectall;
    EditEvent.setfocus;
    exit;
  end;
  if pos('**',EditEvent.text) > 0 then begin
    messagedlg('Events cannot contain double stars',mtinformation,[mbok],0);
    EditEvent.selectall;
    EditEvent.setfocus;
    exit;
  end;
  vars:=TStringList.create;
  Datawell.Explode(EditVariables.text,',',vars);
  form:=TFormVarSelect.create(self);
  with form.List1 do begin
    for i:=0 to vars.count-1 do begin
      if vars[i] = '<@>' then form.BtnAddDummy.click
      else begin
        itemindex:=-1;
        for j:=0 to items.count-1 do begin
          if items[j] = vars[i] then begin
            itemindex:=j;
            break;
          end;
        end;
        if itemindex >= 0 then form.BtnAdd.click;
      end;
    end;
  end;
  form.EditEvent.text:=EditEvent.text;
  form.showmodal;
  if form.vars <> '!XxX!' then EditVariables.text:=form.vars;
  form.free;
  vars.free;
end;

procedure TFormNewEvent.BtnNewClick(Sender: TObject);
var
  didfind: boolean;
  i, amt, stars, commas, highorder: integer;
  work, work2: string;
  table :TClientDataset;
begin
  if EditEvent.text = '' then begin
    messagedlg('You must enter event text',mtinformation,[mbok],0);
    EditEvent.setfocus;
    exit;
  end;
 //Check for "
  if pos('"',EditEvent.text) > 0 then begin
    messagedlg('Events cannot contain quotes',mtinformation,[mbok],0);
    EditEvent.selectall;
    EditEvent.setfocus;
    exit;
  end;
  //Check for **
  if pos('**',EditEvent.text) > 0 then begin
    messagedlg('Events cannot contain double stars',mtinformation,[mbok],0);
    EditEvent.selectall;
    EditEvent.setfocus;
    exit;
  end;
  //Check for filtered *'s
  if (CheckFiltered.checked) and (pos('*',EditEvent.text) > 0) then begin
    messagedlg('Events with stars (*) cannot be filtered',mtinformation,[mbok],0);
    EditEvent.selectall;
    EditEvent.setfocus;
    exit;
  end;
  //Check for variables parsing
  work:=EditEvent.text;
  stars:=0;
  i:=pos('*',work);
  while i > 0 do begin
    inc(stars);
    delete(work,i,1);
    i:=pos('*',work);
  end;
  //Check for too many stars
  if stars > 25 then begin
    messagedlg('You cannot have more than 25 *''s defined in an event',mtinformation,[mbok],0);
    EditEvent.selectall;
    EditEvent.setfocus;
    exit;
  end;
  commas:=0;
  work:=EditVariables.text;
  if work <> '' then begin
    commas:=1;
    i:=pos(',',work);
    while i > 0 do begin
      inc(commas);
      delete(work,i,1);
      i:=pos(',',work);
    end;
  end;
  if stars <> commas then begin
    messagedlg('You must have the same # of *''s in the event as variables selected',
      mtinformation,[mbok],0);
    EditEvent.setfocus;
    exit;
  end;
  amt:=0;
  //Find the currently highest order
  highorder:=0;
  table:=TClientDataset.create(self);
  with table do begin
    cloneCursor(Datawell.TableEvents, false);
    filter := '(Master = '+Datawell.masterindex+') and (FGroup = '''+EditGroup.text+''')';
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      inc(amt);
      if fieldbyname('FOrder').asinteger > highorder then highorder:=fieldbyname('FOrder').asinteger;
      next;
    end;
  end;
  table.free;
  //Add 1 to get the new highest order
  if not BtnSave.visible then inc(highorder); //Only add one if creating a new event
  //Check max # events/group
  if amt > 25 then begin
    messagedlg('You can only have 25 events defined per group',mtinformation,[mbok],0);
    EditEvent.selectall;
    EditEvent.setfocus;
    exit;
  end;
  //Check for existing event
  didfind:=false;
  table:=TClientDataset.create(self);
  with table do begin
    cloneCursor(Datawell.TableEvents, false);
    filter := '(Master = '+Datawell.masterindex+') and (FGroup = '''+EditGroup.text+''')';
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      if fieldbyname('FEvent').asstring = EditEvent.text then begin
        if BtnNew.visible then begin
          didfind:=true;
          break;
        end
        else begin
          if (fieldbyname('FEvent').asstring <> Datawell.TableEvents.fieldbyname('FEvent').asstring) then begin
            didfind:=true;
            break;
          end;
        end;
      end;
      next;
    end;
  end;
  table.free;
  if didfind then begin
    messagedlg('That event already exists for this group.  You must delete it first',
      mtinformation,[mbok],0);
    EditEvent.selectall;
    EditEvent.setfocus;
    exit;
  end;
  if BtnSave.visible then begin //Editing
    with Datawell.TableEvents do begin
      edit;
      fieldbyname('FEvent').asstring:=EditEvent.text;
      work2:=EditVariables.text;
      if CheckDisabled.checked then work2:='|'+work2;
      fieldbyname('FAction').asstring:=work2;
      fieldbyname('Sound').asstring:=EditSound.text;
      fieldbyname('Soundfile').asstring:=EditSound.hint;
      if CheckOriginal.checked then begin
        fieldbyname('FColor').asinteger:=0;
        fieldbyname('BColor').asinteger:=0;
      end
      else begin
        fieldbyname('FColor').asinteger:=EventText.font.color;
        fieldbyname('BColor').asinteger:=EventText.color;
      end;
      fieldbyname('CaseSensitive').asboolean:=CheckCase.checked;
      fieldbyname('Filtered').asboolean:=CheckFiltered.checked;
      fieldbyname('FRetain').asboolean:=CheckRetain.checked;
      fieldbyname('Terminal').asboolean:=CheckTerminal.checked;
      post;
    end;
  end
  else begin //Adding
    with Datawell.TableEvents do begin
      append;
      fieldbyname('Master').asinteger:=Datawell.masterinteger;
      fieldbyname('FGroup').asstring:=EditGroup.text;
      fieldbyname('FEvent').asstring:=EditEvent.text;
      work2:=EditVariables.text;
      if CheckDisabled.checked then work2:='|'+work2;
      fieldbyname('FAction').asstring:=work2;
      fieldbyname('Sound').asstring:=EditSound.text;
      fieldbyname('Soundfile').asstring:=EditSound.hint;
      if CheckOriginal.checked then begin
        fieldbyname('FColor').asinteger:=0;
        fieldbyname('BColor').asinteger:=0;
      end
      else begin
        fieldbyname('FColor').asinteger:=EventText.font.color;
        fieldbyname('BColor').asinteger:=EventText.color;
      end;
      fieldbyname('CaseSensitive').asboolean:=CheckCase.checked;
      fieldbyname('Filtered').asboolean:=CheckFiltered.checked;
      fieldbyname('FRetain').asboolean:=CheckRetain.checked;
      fieldbyname('Terminal').asboolean:=CheckTerminal.checked;
      fieldbyname('FOrder').asinteger:=highorder;
      post;
    end;
  end;
  close;
end;

procedure TFormNewEvent.EditEventKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnSetSound.setfocus;
  end;
end;

procedure TFormNewEvent.EditEventChange(Sender: TObject);
begin
  if pos('*',EditEvent.text) > 0 then begin
{    CheckFiltered.enabled:=false;
    CheckRetain.enabled:=false;
    CheckFiltered.checked:=false;
    CheckRetain.checked:=false;
}
  end
  else begin
    EditVariables.text:='';
    CheckFiltered.enabled:=true;
    CheckRetain.enabled:=true;
  end;
end;

end.

